#include "chatserver.hpp"
#include "chatservice.hpp"
#include "db.h"

#include <cstdlib>
#include <iostream>
#include <csignal>

using namespace std;

//处理服务器ctrl+c服务器后，重置user的状态信息
void resetHandler(int) {
    ChatService::getInstance().reset();
    exit(EXIT_SUCCESS);
}

int main(int argc, char* argv[]) {
    if (argc < 3) {
        cerr << "command invalid! example: ./ChatServer <ip> <port>"; 
        exit(EXIT_FAILURE);
    }

    MySQL::loadConfig();

    char *ip = argv[1];
    uint16_t port = atoi(argv[2]);

    signal(SIGINT, resetHandler);

    EventLoop loop;
    InetAddress addr(ip, port);
    ChatServer server(&loop, addr, "ChatServer");

    server.start();
    loop.loop();    //开启时间循环，服务器就启动了
}